package com.smbms.dao.role;

import cn.hutool.core.util.ReflectUtil;
import com.smbms.pojo.Role;
import com.smbms.pojo.User;
import com.smbms.utils.CaseUtils;
import com.smbms.utils.DBUtils;
import org.junit.Test;

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description 描述
 * @Author 羽落陌殇
 * @Date 2021/7/19 16:56
 * @Version 1.0.0
 */
public class RoleDaoImpl implements RoleDao {
    @Override
    public List<Role> getRoleList(Connection connection) throws SQLException {
        List<Role> roleList = new ArrayList<>();
        String sql = "select * from smbms_role";
        Object[] params = {};
        Role role = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        resultSet = DBUtils.executeQuery(connection, preparedStatement, resultSet, sql, params);
        while (resultSet.next()) {
            role = new Role();
            // 利用反射进行数据值的匹配
            Method[] methods = ReflectUtil.getMethods(Role.class);
            for (int i = 0; i < methods.length; i++) {
                if (methods[i].getName().startsWith("set")) {
                    ReflectUtil.invoke(role, methods[i],
                            resultSet.getObject(CaseUtils.toLowerCaseFirstOne(
                                    methods[i].getName().substring(3))));
                }
            }
            roleList.add(role);
        }
        return roleList;
    }
}
